package com.lagou.edu.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.lagou.edu.service.fallback.EmailServiceHandlersClass;
import com.lagou.edu.util.EmailUtils;
import com.lagou.service.EmailService;
import lombok.SneakyThrows;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;

/**
 * @author tudedong
 * @description
 * @date 2020-07-05 16:40:05
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Value("${config.email.host}")
    private String emailHost;

    @Override
    @SentinelResource(value = "findEmailOpenState",blockHandlerClass = EmailServiceHandlersClass.class,
            blockHandler = "handleException",fallbackClass = EmailServiceHandlersClass.class,fallback = "handleError")
    public Boolean sendEmail(String email, String emailTitle, String message) {

        try {
            long l1 = System.currentTimeMillis();
            //这里邮箱地址和授权码暂时写死，安全性考虑没有放到配置文件中
            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    EmailUtils.sendEmail(emailHost, "562908495@qq.com", "htzhgoivxynsbcja", email, emailTitle, message);
                }
            }).start();

            long l2 = System.currentTimeMillis();
            System.out.println("发送短信验证码所需时长："+(l2-l1));
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
        }
    }
}
